package com.cskaoyan.service.admin;

import com.cskaoyan.bean.admin.Permission;
import com.cskaoyan.bean.admin.PermissionExample;
import com.cskaoyan.bean.admin.PermissionWithSystem;
import com.cskaoyan.bean.admin.PermissionWithSystemExample;
import com.cskaoyan.bean.admin.vo.PermissionsVo;
import com.cskaoyan.mapper.admin.PermissionMapper;
import com.cskaoyan.mapper.admin.PermissionWithSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类<code>Doc</code>用于：TODO 授权管理服务层
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-21
 */
@Service
public class PermissionWithSystemServiceImpl implements PermissionWithSystemService {

    @Autowired
    PermissionWithSystemMapper permissionWithSystemMapper;
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public PermissionsVo queryPermissions(Integer roleId) {

        List<PermissionsVo.SystemPermissionsDTO> systemPermissionList = new ArrayList<>();

        List<PermissionWithSystem> permissionWithSystems = permissionWithSystemMapper.selectByExample(new PermissionWithSystemExample());
        for (PermissionWithSystem permissionWithSystem : permissionWithSystems) {
            if (permissionWithSystem.getLevel() == 0) {// 一级目录
                PermissionsVo.SystemPermissionsDTO systemPermissionsDTO = new PermissionsVo.SystemPermissionsDTO();
                systemPermissionsDTO.setId(permissionWithSystem.getPermissionId());
                systemPermissionsDTO.setLabel(permissionWithSystem.getLabel());
                systemPermissionList.add(systemPermissionsDTO);
                Integer id = permissionWithSystem.getId();
                List<PermissionsVo.SystemPermissionsDTO.ChildrenDTO> childrenDTOList = new ArrayList<>();
                systemPermissionsDTO.setChildren(childrenDTOList);
                for (PermissionWithSystem child : permissionWithSystems) {
                    // 父id=child的pid
                    if (child.getLevel() == 1 && id.equals(child.getPid())) {// 二级目录
                        PermissionsVo.SystemPermissionsDTO.ChildrenDTO childrenDTO = new PermissionsVo.SystemPermissionsDTO.ChildrenDTO();
                        childrenDTO.setId(child.getPermissionId());
                        childrenDTO.setLabel(child.getLabel());
                        childrenDTOList.add(childrenDTO);
                        Integer childId = child.getId();
                        List<PermissionsVo.SystemPermissionsDTO.ChildrenDTO.InnerChildrenDTO> innerChildrenDTOList = new ArrayList<>();
                        childrenDTO.setChildren(innerChildrenDTOList);
                        for (PermissionWithSystem innerChild : permissionWithSystems) {
                            // childId=innerChild的pid
                            if (innerChild.getLevel() == 2 && childId.equals(innerChild.getPid())) {// 三级目录
                                PermissionsVo.SystemPermissionsDTO.ChildrenDTO.InnerChildrenDTO innerChildrenDTO = new PermissionsVo.SystemPermissionsDTO.ChildrenDTO.InnerChildrenDTO();
                                innerChildrenDTO.setId(innerChild.getPermissionId());
                                innerChildrenDTO.setLabel(innerChild.getLabel());
                                innerChildrenDTO.setApi(innerChild.getApi());
                                innerChildrenDTOList.add(innerChildrenDTO);
                            }
                        }
                    }
                }
            }
        }
        // 根据roleId查permission表并添加到assignedPermissions中
        List<String> assignedPermissions = new ArrayList<>();
        PermissionExample example = new PermissionExample();
        if (roleId != 1) {
            example.createCriteria().andRoleIdEqualTo(roleId);
        }
        List<Permission> permissionList = permissionMapper.selectByExample(example);
        permissionList.forEach(item -> assignedPermissions.add(item.getPermission()));
        PermissionsVo permissionsVo = new PermissionsVo();
        permissionsVo.setSystemPermissions(systemPermissionList);
        permissionsVo.setAssignedPermissions(assignedPermissions);
        return permissionsVo;
    }
}
